package com.offcn.service;

import com.offcn.dao.UserDao;
import com.offcn.dao.UserDaoImpl;
import com.offcn.entity.User;
import com.offcn.utils.PageUtils;

import java.util.List;

public class UserServiceImpl implements UserService {

    UserDao userDao = new UserDaoImpl();
    @Override
    public User login(String username, String password) {
        return userDao.login(username,password);
    }

    @Override
    public boolean add(User user) {
        return userDao.add(user)>0?true:false;
    }

    @Override
    public PageUtils<User> findByPage(int currentPage, int pageSize, String search) {
        //一共有几条记录
        int count=userDao.find("select * from user where name like ?","%"+search+"%").size();
        //查出来的那一页的数据
        //limit(（当前页码-1）*页量,页量)
        List<User> userList = userDao.find("select * from user where name like ? limit ?,?",
                "%" + search + "%", (currentPage - 1) * pageSize, pageSize);
        //再把相关的数据整合到工具PageUtils中即可
        PageUtils<User> pageUtils = new PageUtils<>(pageSize,currentPage,count,userList);
        return pageUtils;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public Boolean update(User user) {
        return userDao.update(user)>0?true:false;
    }

    @Override
    public Boolean batchDel(String[] uids) {
        try {
            for (String uid : uids) {
                userDao.deleById(uid);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


}
